#!/usr/bin/env python3.9
# ./sstvmegadatagen.py somethingappropriate.png >image.h

import sys
from PIL import Image

ycbcr = Image.open(sys.argv[1]).convert('YCbCr')
if ycbcr.size != (320,256):
    print ("baka. I can't PD90 with this.")
    sys.exit(1)

width,height = ycbcr.size
y,cb,cr = ycbcr.split()
imY = y.resize((width,height)).convert("F")
imCb = cb.resize((width,height//2)).convert("F")
imCr = cr.resize((width,height//2)).convert("F")

l, cr, cb = [0]*81920, [0]*40960, [0]*40960
y0,y1,y2,y3 = [],[],[],[]

for i in range(81920):
    x = i % 320
    y = i // 320
    yh = y // 2

    pl = imY.getpixel((x,y))
    pr = imCr.getpixel((x,yh))
    pb = imCb.getpixel((x,yh))

    l[i] = int(pl)
    cr[x+320*yh] = int(pr)
    cb[x+320*yh] = int(pb)

for y in range(256):
    sl = l[y*320:]
    sl = sl[:320]
    if y < 128 and (y & 1) == 0: y0 += sl
    if y < 128 and (y & 1) == 1: y1 += sl
    if y > 127 and (y & 1) == 0: y2 += sl
    if y > 127 and (y & 1) == 1: y3 += sl

cr0 = cr[00000:20480]
cr1 = cr[20480:40960]
cb0 = cb[00000:20480]
cb1 = cb[20480:40960]

def spewarrays (label, array):
    s = "const PROGMEM uint8_t " + label + "[20480] = { "
    s += ','.join(map(str,array)) + "};"
    print (s)

spewarrays ("image_y0", y0)
spewarrays ("image_y1", y1)
spewarrays ("image_y2", y2)
spewarrays ("image_y3", y3)
spewarrays ("image_cr0", cr0)
spewarrays ("image_cr1", cr1)
spewarrays ("image_cb0", cb0)
spewarrays ("image_cb1", cb1)

###
